#ifndef _TCPCONNECTION_H
#define _TCPCONNECTION_H
#include "Socket.h"
#include "SocketIO.h"
#include "InetAddress.h"

#include <memory>
#include <functional>
using std::function;
using std::shared_ptr;
// 代表已经建立好的连接
class EventLoop;
class TcpConnection;
using TcpConnectionPtr = shared_ptr<TcpConnection>;
using Callback = function<void(TcpConnectionPtr)>;

class TcpConnection : public std::enable_shared_from_this<TcpConnection>
{
public:
    TcpConnection(int, EventLoop *);
    string receive();
    void send(const string &);
    bool isClosed();
    string toString();
    void shutdown();
    InetAddress getLocalAddress();
    InetAddress getPeerAddress();
    void setAllCallback(const Callback &cb1, const Callback &cb2, const Callback &cb3)
    {
        _onConnection = cb1;
        _onMessage = cb2;
        _onClose = cb3;
    }
    void handlenewConnectionCallback();
    void handleMessageCallback();
    void handleCloseCallback();
    void sendinLoop(const string & msg);
    void sendinLoop(const TLV & tlv);

    //传递读取信息包
    int readPacket(Packet & packet);

private:
    Socket _sock;
    InetAddress _localAddr;
    InetAddress _peerAddr;
    SocketIO _socketIO;
    EventLoop *_loop;
    Callback _onConnection;
    Callback _onMessage;
    Callback _onClose;
};

#endif
